import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { ArrivalNoticeLine, DictApi, MdItemAPI, vendorAPI } from '#/api';

import { reactive } from 'vue';

import { DictResp } from '#/api';

const sys_yes_no = reactive<DictApi.SelectOption[]>([]);
const status = reactive<DictApi.SelectOption[]>([]);
const mes_item_product = reactive<DictApi.SelectOption[]>([]);
DictResp([
  'sys_yes_no',
  'mes_vendor_level',
  'mes_rn_status',
  'mes_item_product',
]).then((res) => {
  if (res.sys_yes_no) {
    sys_yes_no.push(...res.sys_yes_no);
  }
  if (res.mes_rn_status) {
    status.push(...res.mes_rn_status);
  }
  if (res.mes_item_product) {
    mes_item_product.push(...res.mes_item_product);
  }
});

/**
 * 供应商搜索表单的字段配置
 */
export function useVendorScheam() {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入供应商编码',
        allowClear: true,
      },
      fieldName: 'vendorCode',
      label: '供应商编码',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入供应商名称',
      },
      fieldName: 'vendorName',
      label: '供应商名称',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入供应商简称',
      },
      fieldName: 'vendorNick',
      label: '供应商简称',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入供应商英文名称',
      },
      fieldName: 'vendorEn',
      label: '英文名称',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: sys_yes_no,
        placeholder: '是否启用',
      },
      fieldName: 'enableFlag',
      label: '是否启用',
    },
  ];
}

/**
 * 获取供应商表格列配置
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useVendorColumns(
  _onActionClick?: OnActionClickFn<vendorAPI.RowType>,
): VxeTableGridOptions<vendorAPI.RowType>['columns'] {
  return [
    {
      field: 'id',
      fixed: 'left',
      type: 'radio',
      width: 50,
    },
    {
      field: 'vendorCode',
      title: '供应商编码',
    },
    {
      field: 'vendorName',
      title: '供应商名称',
      showOverflow: 'ellipsis',
      width: 200,
    },
    { field: 'vendorNick', title: '供应商简称' },
    {
      field: 'vendorLevel',
      title: '供应商等级',
    },
    { field: 'vendorScore', title: '供应商评分' },
    { field: 'tel', title: '供应商电话' },
    {
      field: 'enableFlag',
      title: '是否启用',
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'remark',
      title: '备注',
    },
  ];
}

/**
 * 物料信息表格字段配置
 */
export function useMaterialColumns(
  _onActionClick?: OnActionClickFn<ArrivalNoticeLine.Row>,
): VxeTableGridOptions<ArrivalNoticeLine.Row>['columns'] {
  return [
    { align: 'left', type: 'checkbox', width: 50 },
    {
      field: 'itemCode',
      title: '物料编码',
      width: 120,
    },
    { field: 'itemName', title: '物料名称' },
    { field: 'specification', title: '规格型号' },
    { field: 'unitName', title: '单位' },
    { field: 'quantityArrival', title: '到货数量' },
    {
      field: 'iqcCheck',
      title: '是否检验',
      cellRender: { name: 'CellTag' },
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          sys_yes_no.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    { field: 'quantityQuanlified', title: '合格数量', width: 80 },
    { field: 'iqcCode', title: '检验单号', width: 80 },
    { field: 'remark', title: '备注', width: 80 },

    {
      title: '操作',
      width: 120,
      slots: { default: 'action' },
    },
  ];
}

/**
 * 物料信息编辑表单的字段配置
 */
export function useMaterialScheam(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'lineId',
      dependencies: {
        triggerFields: ['lineId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'itemId',
      dependencies: {
        triggerFields: ['itemId'],
        show: false,
      },
    },
    {
      component: '',
      fieldName: 'noticeId',
      dependencies: {
        triggerFields: ['noticeId'],
        show: false,
      },
    },
    {
      component: '',
      fieldName: 'itemCode',
      rules: 'required',
      label: '物料编码',
      labelWidth: 70,
      formItemClass: 'col-span-5',
      componentProps: {
        class: 'w-full',
      },
    },
    {
      component: '',
      rules: 'required',
      formItemClass: 'col-span-4',
      componentProps: {
        placeholder: '请输入物料名称',
      },
      labelWidth: 70,
      fieldName: 'itemName',
      label: '物料名称',
    },
    {
      component: '',
      fieldName: 'unitName',
      labelWidth: 50,
      formItemClass: 'col-span-3',
      label: '单位',
      componentProps: {
        placeholder: '',
        class: 'w-full',
        readonly: true,
      },
    },
    {
      component: 'Textarea',
      labelWidth: 70,
      componentProps: {
        placeholder: '请输入规格型号',
        maxLength: 50,
        rows: 2,
        showCount: false,
      },
      formItemClass: 'col-span-8',
      fieldName: 'specification',
      label: '规格型号',
    },

    {
      component: 'ElInputNumber',
      fieldName: 'quantityArrival',
      formItemClass: 'col-span-4',
      defaultValue: 0,
      labelWidth: 70,
      label: '到货数量',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: sys_yes_no,
      },
      defaultValue: 'N',
      fieldName: 'iqcCheck',
      label: '是否检验',
      formItemClass: 'col-span-4',
    },
    {
      component: 'Input',
      fieldName: 'iqcCode',
      label: '来料检验单',
      formItemClass: 'col-span-4',
      disabled: true,
      dependencies: {
        triggerFields: ['iqcCheck'],
        show(value: any) {
          return value.iqcCheck === 'Y';
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'quantityQuanlified',
      label: '合格数量',
      formItemClass: 'col-span-4',

      disabled: true,
      dependencies: {
        triggerFields: ['iqcCheck'],
        show(value: any) {
          return value.iqcCheck === 'Y';
        },
      },
    },

    {
      component: 'Textarea',
      formItemClass: 'col-span-12',
      componentProps: {
        placeholder: '请输入内容',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      labelWidth: 70,
    },
  ];
}

/* 物料产品信息表格数据 */
export function useMaterialProductColumns(
  _onActionClick?: OnActionClickFn<MdItemAPI.Row>,
): VxeTableGridOptions<MdItemAPI.Row>['columns'] {
  return [
    {
      field: 'itemId',
      fixed: 'left',
      type: 'radio',
      width: 50,
    },
    {
      field: 'itemCode',
      title: '物料编码',
    },
    {
      field: 'itemName',
      title: '物料名称',
    },
    {
      field: 'specification',
      title: '规格型号',
    },
    {
      field: 'unitName',
      title: '单位',
    },
    {
      field: 'itemOrProduct',
      title: '物料/产品',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_item_product.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'itemTypeName',
      title: '所属分类',
    },
    {
      field: 'createTime',
      title: '创建时间',
    },
  ];
}

/* 物料产品信息查询表单数据 */
export function useMaterialProductScheam(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'deptId',
      dependencies: {
        triggerFields: ['deptId'],
        show: false,
      },
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入物料编码',
        class: 'mr-5',
      },
      fieldName: 'itemCode',
      label: '物料编码',
      labelClass: 'w-auto text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入物料名称',
      },
      fieldName: 'itemName',
      label: '物料名称',
      labelClass: 'w-auto text-[#606266] font-bold',
    },
  ];
}
